package com.yao.seckill.web.validator;

import com.yao.seckill.web.utils.ValidateUtils;
import com.yao.seckill.web.validator.annotation.IsMobile;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class MobileValidator implements ConstraintValidator<IsMobile,String> {

    private boolean required;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
        required=constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {

        if(required){
            return ValidateUtils.isMobile(value);
        }
        if(StringUtils.isBlank(value))return true;

        return  ValidateUtils.isMobile(value);
    }
}
